package com.jiashihui.schema_lgorithm.java.socket.service2;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.StandardCharsets;
import java.util.List;

public class MessageDecoder extends ByteToMessageDecoder {
    private static final Logger LOGGER = LoggerFactory.getLogger(MessageDecoder.class);

    @Override
    /**
     * 功能描述: <br>
     * 〈decode〉
     *
     * @param: ctx:
     * @param: in:
     * @param: out
     * @return:void
     * @since: 1.0.0
     * @Author:
     * @Date: 2019/05/20 下午 5:24
     */
    protected void decode(ChannelHandlerContext ctx, ByteBuf in,
                          List<Object> out) throws Exception {
        int length = in.readInt();
        int serviceType = in.readInt();
        int messageId = in.readInt();
        byte[] req = new byte[in.readableBytes()];
        LOGGER.debug("报文体长度:{}", req.length);
        in.readBytes(req);
        Message message = new Message();
        message.setLength(length);
        message.setServiceType(serviceType);
        message.setMessageId(messageId);
        message.setData(new String(req, StandardCharsets.UTF_8));
        out.add(message);
    }
}
